/**
 * @Author: LiMeng
 * @Date: 2017/6/6
 */
var GLOBAL = {
    ID: $.getQueryParam("id"),
    ACTION_URL: ROOT_PATH + '/workLicense/fireLicense/edit.shtml',
    EXECUTE_URL: ROOT_PATH + '/workLicense/fireLicense/execute.shtml'
};
var ajaxSuccessHandler = function (result) {
    result = $.parseJSON(result);
    if (result.success) {
        $.messager.progress('close');	// 如果提交成功则隐藏进度条
        layer.msg(result.message, {time: 1000, icon: 1}, function () {
            WindowManager.closeLayer({returnValue: true});
        });
    } else {
        $.messager.progress('close');	// 如果提交成功则隐藏进度条
        layer.msg(result.message, {icon: 2});
    }
};
var addCommonFormParams = function (param) {

};
var formSubmitAction = function (url, params) {
    $.messager.progress();	// 显示进度条

    $('#form1').form('submit', {
        url: url,
        onSubmit: function (param) {
            addCommonFormParams(param);
            if ($.isNotEmpty(params)) {
                $.extend(param, params);
            }
            var isValid = $(this).form('validate');
            if (!isValid) {
                $.messager.progress('close');	// 如果表单是无效的则隐藏进度条
            }
            return isValid;	// 返回false终止表单提交
        },
        success: function (result) {
            ajaxSuccessHandler(result);
        }
    });
};
var toggleFireUserState = function (state) {
    state = state.toString();
    if (state === '1') {
        $('#fireUserNames').textbox('disable').parents('.form_one').addClass('hide');
        $('#fireStaffNames').textbox('enable').parents('.form_one').removeClass('hide');
    } else {
        $('#fireUserNames').textbox('enable').parents('.form_one').removeClass('hide');
        $('#fireStaffNames').textbox('disable').parents('.form_one').addClass('hide');
    }
};
var setFormState = function (data) {
    data = data[0];
    $('#applyId').val(data.id);
    $('#workTypeName').textbox('setValue', data.workTypeName);
    $('#fireLevelId').val(data.workLevelId);
    $('#fireLevelName').textbox('setValue', data.workLevelName);
    $('#planStartTime').datetimebox('setValue', data.planStartDate);
    $('#planEndTime').datetimebox('setValue', data.planEndDate);
    $('#applyUserName').textbox('setValue', data.applyUserName);
    $('#applyUserId').val(data.applyUserId);
    $('#applyDeptName').textbox('setValue', data.applyDeptName);
    $('#applyDeptId').val(data.applyDeptId);
    $('#content').textbox('setValue', data.workPoint + '//' + data.workContent);
    $('#isCheckContractor').val(data.isCheckContractor);
    if (data.isCheckContractor === 1) {//承包商
        $('#deptId').val(data.contractorAuditId);
        $('#deptName').textbox('setValue', data.contractorAudit.contractorName);
        $('#contractorId').val(data.contractorAudit.contractorId);
        toggleFireUserState(1);
    } else {
        $('#deptId').val(data.deptId);
        $('#deptName').textbox('setValue', data.deptName);
        toggleFireUserState(0);
    }
};
var getSelectedContractorStaff = function (data, target) {
    if (target === null || target === '') {
        return;
    }
    var names = '';
    var ids = '';
    for (var index in data) {
        names += ',' + data[index].name;
        ids += ',' + data[index].id;
    }

    $('#' + target + 'Names').textbox('setValue', names.substring(1));
    $('#' + target + 'Ids').val(ids.substring(1));
};
var openSelectContractorStaff = function (title, target) {
    var contractorId = $('#contractorId').val();
    if (contractorId === '' && target !== 'watchStaff') {
        layer.alert('请先选择用火单位！');
        return;
    }
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['700px', '400px'],
        content: ROOT_PATH + '/contractor/contractorStaff/simpleList.shtml?isMulti=1&contractorId=' + contractorId,
        callBack: function (data) {
            getSelectedContractorStaff(data, target);
        }
    });
};
var getSelectedFireUser = function (data) {
    var names = '';
    var ids = '';
    for (var index in data) {
        names += ',' + data[index].realName;
        ids += ',' + data[index].id;
    }

    $('#fireUserNames').textbox('setValue', names.substring(1));
    $('#fireUserIds').val(ids.substring(1));
};
var openSelectUser = function (title, callback) {
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['900px', '500px'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=1',
        callBack: callback
    });
};
$(function () {
    var WORK_TYPE_ID = $('#workTypeId').val();
    var IS_CHECK_CONTRACTOR = $('#isCheckContractor').val();
    var APPLY_ID = $('#applyId').val();
    toggleFireUserState(IS_CHECK_CONTRACTOR);

    $('#workTypeName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                WindowManager.openLayer({
                    type: 2,
                    title: '选择作业申请',
                    area: ['1150px', '500px'],
                    content: ROOT_PATH + '/workLicense/apply/simpleList.shtml?workTypeId=' + WORK_TYPE_ID + '&applyId=' + APPLY_ID,
                    callBack: setFormState
                });
            }
        }],
        editable: false
    });

    $('#dangerTypeIds').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.DANGER_TYPE,
        valueField: 'id',
        editable: false,
        multiple: true,
        required: true
    });

    $('#fireUserNames').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择用火人', getSelectedFireUser)
            }
        }],
        editable: false
    });

    $('#fireStaffNames').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectContractorStaff('选择用火人', 'fireStaff');
            }
        }],
        editable: false
    });

    $('#watchStaffNames').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectContractorStaff('选择监火人', 'watchStaff');
            }
        }],
        editable: false
    });

    $('#btn_save').on('click', function () {
        formSubmitAction(GLOBAL.ACTION_URL);
    });
    $('#btn_execute').on('click', function () {
        var index = layer.confirm("确定执行本条作业许可信息吗？", function () {
            $.messager.progress();
            $.post(GLOBAL.EXECUTE_URL, {
                id: GLOBAL.ID
            }).done(function (result) {
                ajaxSuccessHandler(result);
                layer.close(index);
            });
        });
    });
});